#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
#    pyqtsozluk, qtsozluk and pysozluk mix dictionary application
#    Copyright (C) 2007  pyqtsozluk authors
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

from PyQt4 import QtGui, QtCore
from optparse import OptionParser
from sys import exit, argv
from pysozlukglobals import version as progversion
from core import searcherThread
from online import onlineDatabase
from database import pysozlukDatabase

#    Command Line Interface for PyQtSozluk
#    Usage is simple, -s or -m followed by the word that will be translated
#    -g and -o are options for gui mode and on/offline search


def search(keyword, gui_stat, online_stat):
    if gui_stat:
        a = QtGui.QApplication([])
        ob = searcherThread(keyword, not online_stat, 0)
        browser = QtGui.QTextBrowser()
        QtCore.QObject.connect(ob, QtCore.SIGNAL("finishedSearching"), browser.setHtml)
        ob.start()
        browser.move(QtGui.QCursor.pos())
        browser.show()
        a.exec_()
    else:
        if online_stat:
            database = onlineDatabase()
            results = database.search(keyword, threaded=False)
            for result in results:
                print result.text
                exit
        else:
            database = pysozlukDatabase()
            results = database.search(keyword, threaded=False)
            print u"Search result: "
            for result in results:
                print result.text
                exit()


def smartsearch(keyword, gui_stat):
    if gui_stat:
        a = QtGui.QApplication([])
        ob = searcherThread(keyword, 0, 1)
        browser = QtGui.QTextBrowser()
        QtCore.QObject.connect(ob, QtCore.SIGNAL("finishedSearching"), browser.setHtml)
        ob.start()
        browser.move(QtGui.QCursor.pos())
        browser.show()
        a.exec_()
    else:
        database = pysozlukDatabase()
        # complete please


def main():

    gui_enabled = 0
    online = 0

    use = """pyqtsozlukcli [option] word
            sample:
            pyqtsozlukcli -s word
            pyqtsozlukcli -s word -g -o
            pyqtsozlukcli -m word -g
          """

    desc = "PyQtSozluk Command Line Interface"

    parser = OptionParser(usage=use, version=progversion, description=desc)
    parser.add_option("-g", "--gui", help="Output to graphical user interface", action="store_true", dest="gui_status", default=False)
    parser.add_option("-o", "--online", help="Search online", action="store_true", dest="online_search", default=False)
    parser.add_option("-m", "--smart", help="Translate corresponding word in smart mode", action="store", dest="smartword")
    parser.add_option("-s", "--search", help="Translate corresponding word", action="store", dest="searchword")

    (options, args) = parser.parse_args()
    if not args and not options: parser.exit()

    if options.gui_status:
        gui_enabled = 1

    if options.online_search:
        online = 1

    if options.smartword == None and options.searchword != None:
        search(options.searchword, gui_enabled, online)
    elif options.smartword != None and options.searchword == None:
        smartsearch(options.smartword, gui_enabled)

if __name__ == "__main__":
    main()
